package com.muyu.danmo.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.muyu.common.core.domain.Result;
import com.muyu.danmo.domain.DanmoInformationMass;
import com.muyu.danmo.domain.DanmoInformationMassPhotos;
import com.muyu.danmo.domain.DanmoPersonInfo;
import com.muyu.danmo.mapper.DanmoInformationMassMapper;
import com.muyu.danmo.mapper.DanmoInformationMassPhotosMapper;
import com.muyu.danmo.mapper.DanmoPersonInfoMapper;
import com.muyu.danmo.service.IDanmoInformationMassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 社会团体中的小社团Service业务层处理
 * 
 * @author yangle
 * @date 2024-11-13
 */
@Service
public class DanmoInformationMassServiceImpl extends ServiceImpl<DanmoInformationMassMapper, DanmoInformationMass> implements IDanmoInformationMassService
{
    @Autowired
    private DanmoInformationMassMapper danmoInformationMassMapper;

    /**
     * 查询社会团体中的小社团
     * 
     * @param massId 社会团体中的小社团主键
     * @return 社会团体中的小社团
     */
    @Override
    public DanmoInformationMass selectDanmoInformationMassByMassId(Long massId)
    {
        return danmoInformationMassMapper.selectDanmoInformationMassByMassId(massId);
    }

    /**
     * 查询社会团体中的小社团列表
     * 
     * @param danmoInformationMass 社会团体中的小社团
     * @return 社会团体中的小社团
     */
    @Override
    public List<DanmoInformationMass> selectDanmoInformationMassList(DanmoInformationMass danmoInformationMass)
    {
        return danmoInformationMassMapper.selectDanmoInformationMassList(danmoInformationMass);
    }

    @Autowired
    private DanmoInformationMassPhotosMapper danmoInformationMassPhotosMapper;
    /**
     * 新增社会团体中的小社团
     *
     * @param danmoInformationMass 社会团体中的小社团
     * @return 结果
     */
    @Override
    public Result insertDanmoInformationMass(DanmoInformationMass danmoInformationMass)
    {
        effectiveness(danmoInformationMass);
//        try{
            danmoInformationMassMapper.insertDanmoInformationMass(danmoInformationMass);
            Long massId = danmoInformationMass.getMassId();
            if ( danmoInformationMass.getDanmoInformationMassPhotosList()!=null){
                for (DanmoInformationMassPhotos danmoInformationMassPhotos : danmoInformationMass.getDanmoInformationMassPhotosList()) {
                    danmoInformationMassPhotos.setMassId(massId);
                    danmoInformationMassPhotosMapper.insertDanmoInformationMassPhotos(danmoInformationMassPhotos);
                }
            }
             return  Result.success(danmoInformationMass,"添加成功");
//        }catch (Exception e){
//            return  Result.error("添加失败");
//        }
    }

    private Result effectiveness(DanmoInformationMass danmoInformationMass) {
        if (danmoInformationMass.getMassName()==null){
            return Result.error("请输入社团名称");
        }
        if (danmoInformationMass.getMassPicture()==null){
            return Result.error("请上传社团图片");
        }
        if (danmoInformationMass.getMassType()==null){
            return Result.error("请选择社团类型");
        }
        if (danmoInformationMass.getAssociation()==null){
            return Result.error("请选择协会");
        }
        if (danmoInformationMass.getDetails()==null){
            return Result.error("请输入社团详情");
        }
        if (danmoInformationMass.getProvince()==null){
            return Result.error("请选择省份");
        }
        if (danmoInformationMass.getCitycode()==null){
            return Result.error("请选择市");
        }
        if (danmoInformationMass.getCountycode()==null){
            return Result.error("请选择县");
        }

        return null;
    }

    /**
     * 修改社会团体中的小社团
     * 
     * @param danmoInformationMass 社会团体中的小社团
     * @return 结果
     */
    @Override
    public Result updateDanmoInformationMass(DanmoInformationMass danmoInformationMass)
    {
        try {
            danmoInformationMassMapper.updateDanmoInformationMass(danmoInformationMass);

            Long massId = danmoInformationMass.getMassId();
            DanmoInformationMassPhotos danmoInformationMassPhotos = new DanmoInformationMassPhotos();
            danmoInformationMassPhotos.setMassId(massId);
            Long [] ids = danmoInformationMass.getDanmoInformationMassPhotosList().stream().map(DanmoInformationMassPhotos::getId).toArray(Long[]::new);
//           if (ids.length>0){
//               danmoInformationMassPhotosMapper.deleteDanmoInformationMassPhotosByIds(ids);
//           }
            danmoInformationMassPhotosMapper.deleteDanmoInformationMassPhotosByIds(ids);
            for (DanmoInformationMassPhotos danmoInformationMassPhotos1 : danmoInformationMass.getDanmoInformationMassPhotosList()) {
                danmoInformationMassPhotos1.setMassId(massId);
                danmoInformationMassPhotosMapper.insertDanmoInformationMassPhotos(danmoInformationMassPhotos1);
            }

            return Result.success(danmoInformationMass,"修改成功");
        }catch  (Exception e) {
            return Result.error("修改失败");
        }
    }

    /**
     * 批量删除社会团体中的小社团
     * 
     * @param massIds 需要删除的社会团体中的小社团主键
     * @return 结果
     */
    @Override
    public Result deleteDanmoInformationMassByMassIds(Long[] massIds)
    { try {
         danmoInformationMassMapper.deleteDanmoInformationMassByMassIds(massIds);
        return Result.success(massIds,"删除成功");
    }catch  (Exception e) {
        return Result.error("删除失败");
    }
    }

    /**
     * 删除社会团体中的小社团信息
     * 
     * @param massId 社会团体中的小社团主键
     * @return 结果
     */
    @Override
    public Result deleteDanmoInformationMassByMassId(Long massId)
    {
        try {
            danmoInformationMassMapper.deleteDanmoInformationMassByMassId(massId);

            DanmoInformationMassPhotos danmoInformationMassPhotos = new DanmoInformationMassPhotos();
            danmoInformationMassPhotos.setMassId(massId);
            for (DanmoInformationMassPhotos informationMassPhotos : danmoInformationMassPhotosMapper.selectDanmoInformationMassPhotosList(danmoInformationMassPhotos)) {
                danmoInformationMassPhotosMapper.deleteDanmoInformationMassPhotosById(informationMassPhotos.getId());
            }

        return Result.success(massId,"删除成功");
    }catch  (Exception e) {
        return Result.error("删除失败");
    }
    }
}
